#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include "BasicServerEx.h"
#include "IPacketParser.h"
#include "CurlManager.h"
using namespace SGLib;
using namespace std;

#include "GameSession.h"
#include "HttpCmdSession.h"
#include "PacketParser.h"
#include "RobotUser.h"
#include <google/protobuf/stubs/common.h>
IServerEx *gsvr = NULL;
extern string GAME_URL;

static void handler(int sigal) {}

static void timercall_HeartBeat(void *pData, s32 nDataLen)
{
    CRobotUserManager::Instance().RobotsHeartBeat();
}

int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    srand( time(NULL) );
    LOG_INIT( "test.log", CLogExManager::E_LogMode_Day, CLogExManager::E_LogLevel_Debug, CLogExManager::E_LogLevel_Error );

    s32 httpport = 22222;
    if( argc > 1 )
    {
        httpport = atoi( argv[1] );
    }
    if( argc > 2 )
    {
        GAME_URL = argv[2];
    }

    CCurlManager::Instance().Init(1);
    if( CCurlManager::Instance().Start() )
    {
        ServiceObj<CGameSocket, CSGPacketParser> objgame(1, 0, 1000, 64*1024, 64*1024);
        ServiceObj<CHttpCmdSocket, CDefaultPacketParser> objcmd(2, httpport, 100, 128*1024, 128*1024);
        IServiceObj *objs[2] = {&objgame, &objcmd};
        gsvr = new CBasicServerEx<1000>(2, objs);
        if( !gsvr->Start() )
        {
            LOG_ERROR("start failed");
            return 0;
        }

        // 启动机器人心跳的时钟
        s32 tid = gsvr->AddTimer(30000, timercall_HeartBeat, NULL, 0, true);
        LOG_INFO("robot server start. timer[%d] GAME_URL[%s]", tid, GAME_URL.c_str());
        signal( SIGTERM, handler );
        pause();
        gsvr->Stop();
        CCurlManager::Instance().Stop();
    }

    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

